package com.goparry.taskman.restclient;

import java.util.List;

import com.goparry.taskman.repository.RObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

/**
 * @author David Parry
 * This is a "template" class that extends the android AsyncTask by adding
 * an optional ProgressDialog. The class has two constructors:
 * 
 * 1. DoStuff(), does not display a progress dialog
 * 2. DoStuff(context), which does display a progress dialog and must be called from an activity.
 * 
 */
public class AsyncRestClientRObjects extends AsyncTask<String, Integer, List<RObject>>{
	ProgressDialog mProgressDialog;
	Context mContext;
	
	public AsyncRestClientRObjects() {
		mContext = null;		
	}
	public AsyncRestClientRObjects(Context context) {
		mContext = context;		
	}

	public void initialize(Context context) {
		mContext = context;
	}
	@Override
	protected void onPreExecute() {
		if (mContext != null) {
	        mProgressDialog= new ProgressDialog(mContext);
	        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	        mProgressDialog.setMessage("Loading task data...");
	        mProgressDialog.setIndeterminate(true);
			mProgressDialog.show();
		}
		super.onPreExecute();
	}
	protected List<RObject> doInBackground(String... arg0) {
		XmlPullRObjectsParser parser = new XmlPullRObjectsParser(arg0[0]);
		List<RObject> rObjects = parser.parse();
		return rObjects;
	}
    protected void onProgressUpdate(Integer... progress) {
    	//mProgressDialog.setProgress(progress[0]);
    }
	@Override
	protected void onPostExecute(List<RObject> result) {
		if (mContext != null) {
			mProgressDialog.dismiss();
		}
		super.onPostExecute(result);
	}
}
